<?php
define("HDPHP_PATH", 1);
if (is_file('lock.php')) {
    header("Content-type:text/html;charset=utf-8");
    echo "<div style='color:red;font-size:35px;'>请删除install/lock.php文件后安装</div>";
    exit;
}
if (empty($_POST)) {
    require "install.html";
} else {
    //修改配置项
    $config = array_merge(require '../db.inc.php', $_POST);
    file_put_contents('../db.inc.php',
        "<?php if(!defined('HDPHP_PATH'))EXIT;\nreturn " . var_export($config, true) . "\n;?>");
    //修改数据库
    mysql_connect($config['DB_HOST'], $config['DB_USER'], $config['DB_PASSWORD']);
    mysql_query("SET NAMES UTF8");
    mysql_query("CREATE DATABASE IF NOT EXISTS " . $config['DB_DATABASE']);
    mysql_select_db($config['DB_DATABASE']);
    //创建表
    $sql = file_get_contents('../hd_question.sql');
    $sql = explode(';', $sql);
    foreach ($sql as $s) {
        mysql_query($s);
    }
    //修改团队名
    $sql = "UPDATE team SET title='{$_POST['team1']}' WHERE team='a'";
    mysql_query($sql);
    $sql = "UPDATE team SET title='{$_POST['team2']}' WHERE team='b'";
    mysql_query($sql);
    touch("lock.php");
    header("location:../index.php");
}